package com.mrobuy.jdvc.dao.impl;

import com.mrobuy.jdvc.dao.TaskDao;
import com.mrobuy.jdvc.dao.VCProductDao;
import com.mrobuy.jdvc.entity.Task;
import com.mrobuy.jdvc.entity.VCProduct;
import com.mrobuy.util.Pagination;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Repository("taskDao")
public class TaskDaoImpl extends CommonDaoImpl implements TaskDao {

    private static Logger LOG = LoggerFactory.getLogger(TaskDao.class);

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public List<Task> list() {

        StringBuilder sql = new StringBuilder();
        sql.append(" SELECT * from TASK");
        try {
            List<Task> list = jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper(Task.class));
            return list;
        } catch (Exception e) {
            LOG.error(e.getMessage());
        }

        return Collections.EMPTY_LIST;
    }

    @Override
    public Pagination load(int start, int length, Task condition) {
        Pagination pagination = new Pagination();

        List<Object> params = new ArrayList<>();

        StringBuilder whereCond = new StringBuilder(" where 1 =1 ");

        if ( condition.getTitle()!=null && condition.getTitle().trim().length() > 0 ) {
            whereCond.append("  and TITLE like ? ");
            params.add("%" + condition.getTitle().trim() + "%");
        }

        if ( condition.getStatus()!=null && condition.getStatus().trim().length() > 0 ) {
            whereCond.append(" and STATUS = ? ");
            params.add(condition.getStatus().trim() );
        }

        if ( condition.getType()!=null && condition.getType().length() > 0 ) {
            whereCond.append(" and TYPE = ? ");
            params.add(condition.getType().trim() );
        }

        if ( condition.getFlag()!=null && condition.getFlag().length() > 0 ) {
            whereCond.append(" and FLAG = ? ");
            params.add(condition.getFlag().trim() );
        }

        try {

            int count = jdbcTemplate.queryForObject("select count(*) from TASK " + whereCond.toString(), params.toArray(new Object[params.size()]), Integer.class);

            params.add(start);
            params.add(length);
            List<Task> list = jdbcTemplate.query("select * from TASK " + whereCond.toString() + " limit ?, ?", params.toArray(new Object[params.size()]), new BeanPropertyRowMapper(Task.class));


            pagination.setRecordsTotal(count);
            pagination.setRecordsFiltered(count);
            pagination.setData(list);
        } catch (Exception e) {
            LOG.error(e.getMessage());
        }

        return pagination;
    }

    @Override
    public Task load(String taskid) {
        try {
            Task task = (Task)jdbcTemplate.queryForObject("select * from TASK where ID = ?", new Object[]{taskid}, new BeanPropertyRowMapper(Task.class));
            return task;
        } catch (Exception e) {
            LOG.error(e.getMessage());
        }

        return null;
    }
}
